import numpy as np
import time
from IPython import display
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

Bao = []
boardsize = 30
for i in range(boardsize):
	for j in range(boardsize):
		Bao.append([i, j])

N = np.zeros((boardsize , boardsize))



P = []
for i in range(boardsize*boardsize):
	P.append((i+1)/(boardsize*boardsize))





k = -1  # k是上面列表N的索引控制值
l = 0

tpi = []
tpj = []

def up(i, j):  # 负责控制团簇向上方向遍历
	global l
	global k
	if i == -boardsize-1:
		i = boardsize-1
	if new_arr[i][j] == 0:
		N[l][k] = N[l][k] + 1
		new_arr[i][j] = 2
		e = i
		f = j - 1
		left(e, f)
		g = i
		h = j + 1
		right(g, h)
		i = i - 1
		up(i, j)
	else:
		N[l][k] = N[l][k]

def up2(i, j):  # 负责控制团簇向上方向遍历
	if i == -boardsize-1:
		i = boardsize-1
	if new_arr[i][j] == 2:
		new_arr[i][j] = 3
		e = i
		f = j - 1
		left2(e, f)
		g = i
		h = j + 1
		right2(g, h)
		i = i - 1
		up2(i, j)


def up3(i, j):  # 负责控制团簇向上方向遍历
	if i == -boardsize-1:
		i = boardsize-1
	if new_arr[i][j] == 2:
		new_arr[i][j] = 4
		e = i
		f = j - 1
		left3(e, f)
		g = i
		h = j + 1
		right3(g, h)
		i = i - 1
		up3(i, j)




def down(i, j):  # 负责控制团簇向下方向遍历
	global k
	global l
	if i == boardsize:
		i = -boardsize
	if new_arr[i][j] == 0:
		N[l][k] = N[l][k] + 1
		new_arr[i][j] = 2
		e = i
		f = j - 1
		left(e, f)
		g = i
		h = j + 1
		right(g, h)
		i = i + 1
		down(i, j)
	else:
		N[l][k] = N[l][k]


def down2(i, j):  # 负责控制团簇向下方向遍历
	if i == boardsize:
		i = -boardsize
	if new_arr[i][j] == 2:
		new_arr[i][j] = 3
		e = i
		f = j - 1
		left2(e, f)
		g = i
		h = j + 1
		right2(g, h)
		i = i + 1
		down2(i, j)


def down3(i, j):  # 负责控制团簇向下方向遍历
	if i == boardsize:
		i = -boardsize
	if new_arr[i][j] == 2:
		new_arr[i][j] = 4
		e = i
		f = j - 1
		left3(e, f)
		g = i
		h = j + 1
		right3(g, h)
		i = i + 1
		down3(i, j)



def left(i, j):  # 负责控制团簇向左方向遍历
	global k
	global l
	if j == -boardsize-1:
		j = boardsize-1
	if new_arr[i][j] == 0:
		N[l][k] = N[l][k] + 1
		new_arr[i][j] = 2
		a = i - 1
		b = j
		up(a, b)
		c = i + 1
		d = j
		down(c, d)
		j = j - 1
		left(i, j)
	else:
		N[l][k] = N[l][k]


def left2(i, j):  # 负责控制团簇向左方向遍历
	if j == -boardsize-1:
		j = boardsize-1
	if new_arr[i][j] == 2:
		new_arr[i][j] = 3
		a = i - 1
		b = j
		up2(a, b)
		c = i + 1
		d = j
		down2(c, d)
		j = j - 1
		left2(i, j)

def left3(i, j):  # 负责控制团簇向左方向遍历
	if j == -boardsize-1:
		j = boardsize-1
	if new_arr[i][j] == 2:
		new_arr[i][j] = 4
		a = i - 1
		b = j
		up3(a, b)
		c = i + 1
		d = j
		down3(c, d)
		j = j - 1
		left3(i, j)


def right(i, j):  # 负责控制团簇向右方向遍历
	global k
	global l
	if j == boardsize:
		j = -boardsize
	if new_arr[i][j] == 0:
		N[l][k] = N[l][k] + 1
		new_arr[i][j] = 2
		a = i - 1
		b = j
		up(a,b)
		c = i + 1
		d = j
		down(c,d)
		j = j + 1
		right(i,j)
	else:
		N[l][k] = N[l][k]



def right2(i, j):  # 负责控制团簇向右方向遍历
	if j == boardsize:
		j = -boardsize
	if new_arr[i][j] == 2:
		new_arr[i][j] = 3
		a = i - 1
		b = j
		up2(a, b)
		c = i + 1
		d = j
		down2(c, d)
		j = j + 1
		right2(i, j)

def right3(i, j):  # 负责控制团簇向右方向遍历
	if j == boardsize:
		j = -boardsize
	if new_arr[i][j] == 2:
		new_arr[i][j] = 4
		a = i - 1
		b = j
		up3(a, b)
		c = i + 1
		d = j
		down3(c, d)
		j = j + 1
		right3(i, j)



def change_color(max):
	i = max[0][0]
	j = max[1][0]
	new_arr[i][j] = 3
	a = i - 1
	b = j
	up2(a, b)
	c = i + 1
	d = j
	down2(c, d)
	e = i
	f = j - 1
	left2(e, f)
	g = i
	h = j + 1
	right2(g, h)

def change_color2(max2):
	i = max2[0][0]
	j = max2[1][0]
	new_arr[i][j] = 4
	a = i - 1
	b = j
	up3(a, b)
	c = i + 1
	d = j
	down3(c, d)
	e = i
	f = j - 1
	left3(e, f)
	g = i
	h = j + 1
	right3(g, h)




def Sagiri(my_board):
	global N
	global k
	global l
	global new_arr
	global max,max2
	#global  tpi,tpj

	#for q in range(1):
	bao = random.choice(Bao)
	my_board[bao[0]][bao[1]] = 0
	Bao.remove(bao)
	#print(bao,bao[0],bao[1])



	for i in range(0, boardsize, 1):
		for j in range(0, boardsize, 1):
			if my_board[i][j] == 3 or my_board[i][j] == 2 or my_board[i][j] == 4 or my_board[i][j] == 6:
				my_board[i][j] = 0


	new_arr = my_board

	#print('my_board:',my_board)

	for i in range(0, boardsize, 1):
		for j in range(0, boardsize, 1):
			l = i
			k = j
			if new_arr[i][j] == 0:
				N[l][k] = N[l][k] + 1
				new_arr[i][j] = 2
				a = i - 1
				b = j
				up(a, b)
				c = i + 1
				d = j
				down(c, d)
				e = i
				f = j - 1
				left(e, f)
				g = i
				h = j + 1
				right(g, h)
				#print('new_Arr:',my_board)
				#print('N',N)
			if new_arr[i][j] == 1:  # 当矩阵中值为1时，不计入团簇，并结束该次循坏
				continue
	#print('N', N)
	max = np.where(N == np.max(N))

	change_color(max)

	# 暂存最大团簇的坐标值，并暂时消除最大团簇的影响，方便计算次大团簇
	for i in range(0, boardsize, 1):
		for j in range(0, boardsize, 1):

			if my_board[i][j] == 2:
				my_board[i][j] = 0

	N = np.zeros((boardsize , boardsize ))
	#print('M=',M)
	#print('N=',N)

	for i in range(0, boardsize, 1):
		for j in range(0, boardsize, 1):
			l = i
			k = j
			if new_arr[i][j] == 0:
				N[l][k] = N[l][k] + 1
				new_arr[i][j] = 2
				a = i - 1
				b = j
				up(a, b)
				c = i + 1
				d = j
				down(c, d)
				e = i
				f = j - 1
				left(e, f)
				g = i
				h = j + 1
				right(g, h)
				#print('new_Arr:',new_arr)
				#print('N[l][k]',N)
			if new_arr[i][j] == 1:  # 当矩阵中值为1时，不计入团簇，并结束该次循坏
				continue
	max2 = np.where(N == np.max(N))

	change_color2(max2)
# Some helper functions

# Initialize the board with starting positions




o = 0
p = -1

def update_board(my_board):
	global N
	global k
	global l
	global new_arr
	global max
	global o,p

	Sagiri(my_board)







	#print('max=',max)
	#print('N=', N)

	N = np.zeros((boardsize , boardsize ))
	#print('++++++++++++')
	new_board = new_arr
	#print(new_board)
	#print(new_board[3])
	return new_board



# Input variables for the board
# board will be X by X where X = boardsize
initial_cells = 1 # this number of initial cells will be placed in randomly generated positions

my_board = np.ones((boardsize , boardsize ))
pos_list = []













#????????????????????
update_board(my_board)




##### Animate the board #####
# This will throw an error the first time you run the code, but the program will run properly if you
# execute the cell again (there is an error with the animation package that I cannot seem to get rid of)

def animate(num):
	#print('1-----------------')
	#print('frame=',frame)
	im.set_array(update_board(my_board))




	plt.title('我P=%.4f'%P[num])
	#print('2-------------------')
	return im


# matplotlib notebook
#fig = plt.gcf() #创建画布
fig = plt.figure() #创建画布

im = plt.imshow(my_board, animated=True)


# 在开头加如下代码。其中，第一句的'Droid Sans Fallback'是Ubuntu自带的汉字字体，第二句的作用是正常显示负号。



anim = animation.FuncAnimation(fig, func=animate, frames=20000, interval=0.001, blit=False)
anim.save('Baos.gif')
plt.show()



#print(pos_list)
#print(my_board)
#print(pos_list[0][0],pos_list[0][1],pos_list[1][1])


